<%@ page import="com.feng.ALittleFlower.Database" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<%
    String username = (String) request.getParameter("username");
    String password = (String) request.getParameter("password");
    String usernameProblem = "";
    String passwordProblem = "";
    boolean passed = (username != null && password != null);
    if (username != null) {
        try {
            if (!Database.isSQLUserExists(username)) {
                usernameProblem = "该用户名" + username + "不存在";
                passed = false;
            } else if (password != null) {
                if (!Database.isPasswordMatched(username, password)) {
                    passwordProblem = "密码不正确";
                    passed = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    if (passed) {
        try {
            Database.createAccount(username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        session.setAttribute("username", username);
        response.sendRedirect("./index.jsp");
    }
%>
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link href="./css/bootstrap.css" rel="stylesheet">
    <link href="register.css" rel="stylesheet">
    <title>Hello, world!</title>
</head>

<body class="bg d-flex">
<%@include file="navbar.jsp" %>
<div class="container my-5 py-5">
    <div class="row">
        <div class="col-12 text-center">
            <div class="col-12 ">
                <h1 class="display-4 text-primary">登陆</h1>
                <hr class="w-25 mx-auto">
            </div>
        </div>
    </div>
    <form class="form_container" method="post">
        <div class="mb-3">
            <input placeholder="请输入用户名" required type="text" class="form-control" name="username" id="username"
                   aria-describedby="usernameHelp">
            <div id="usernameHelp" class="alert-danger"><%=usernameProblem%>
            </div>
        </div>
        <div class="mb-3">
            <input type="password" required placeholder="请输入密码" class="form-control" name="password" id="password"
                   aria-describedby="passwordHelp">
            <div id="passwordHelp" class="alert-danger"><%=passwordProblem%>
            </div>
        </div>

        <button type="submit" class="btn btn-primary btn-lg">登陆</button>
    </form>
</div>


<script src="js/popper.js"></script>
<script src="js/bootstrap.js"></script>
</body>

</html>